package com.ceshiren.test.houduankaifa.controller;

import com.ceshiren.test.houduankaifa.dto.UserDto;
import com.ceshiren.test.houduankaifa.dto.UserXMLDto;
import com.ceshiren.test.houduankaifa.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.awt.*;

@Api(tags = "BasePostController: controller用户登录post请求类")
@RestController
public class BasePostController {


    /**
     * 基础参数请求
     *
     * @param username
     * @param password
     * @return
     */
    @ApiOperation("最简单的登录，未做任何校验")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "登陆用户名"),
            @ApiImplicitParam(name = "password",value = "登陆密码"),
    })
    @PostMapping("/login")
    public String login(String username, String password) {
        return "登陆场成功！ 登录的用户名为：" + username + "密码为：" + password;
    }



    /**
     * json请求
     */
    @PostMapping(value = "/login2", produces = "application/json")
    public String login2(@RequestBody UserDto userDto) {
        if (userDto.getUsername().equals("ceshiren") && userDto.getPassword().equals("123456")) {
            return "登陆成功！！！登录的用户名为：" + userDto.getUsername() + "密码为：" + userDto.getPassword();
        } else {
            return "登陆失败!!!登录的用户名为：" + userDto.getUsername() + "密码为：" + userDto.getPassword();
        }
    }

    /**
     * Json请求，加上多个参数
     *
     * @param module
     * @param userDto
     * @param desc
     * @param age
     * @return
     */
    @PostMapping(value = "/{module}/register", produces = "application/json")
    public String registerAndParam(@PathVariable String module, @RequestBody UserDto userDto,
                                   @RequestParam String desc, @RequestParam String age) {
        return "注册模块：" + module + "，\n注册用户名：" + userDto.getUsername() + ",\n注册密码：" + userDto.getPassword() + ",\n用户年龄：" + age + ",\n用户备注：" + desc;
    }

    /**
     * xml请求
     * 步骤：
     * 1、xml格式解析，第三方包导入  jackson-dataformat-xml  2.13.2
     * 2、创建对应的xml解析实体类
     * （1）实体类名对应xml根标签  实体类上添加注解 @JacksonXmlRootElement
     * （2）实体类成员变量对应xml内标签名
     * 3、编写post请求并声明 produces = MediaType.APPLICATION_XML_VALUE
     * 4、启动服务，postman输入验证
     */
    @PostMapping(value = "/{module}/register", produces = MediaType.APPLICATION_ATOM_XML_VALUE)
    public String registerXmlAndParam(@PathVariable String module, @RequestBody UserXMLDto userXMLDto,
                                      @RequestParam String desc, @RequestParam String age) {
        return "注册模块：" + module + "，\n注册用户名：" + userXMLDto.getUname() + ",\n注册密码：" + userXMLDto.getUpwd() + ",\n用户年龄：" + age + ",\n用户备注：" + desc;
    }


    @Autowired
    UserService userService;

    @PostMapping(value = "/login3", produces = "application/json")
    public String login3(@RequestBody UserDto userDto) {
        return userService.login(userDto);
    }

    @PostMapping(value = "/{module}/register2", produces = "application/json")
    public String registerAndParam2(@PathVariable String module, @RequestBody UserDto userDto,
                                    @RequestParam String desc, @RequestParam String age) {
        String s = userService.register(userDto);
        return "注册模块：" + module + "，\n" + s + ",\n用户年龄：" + age + ",\n用户备注：" + desc;
    }

}
